//
//  DiscussDetailCell.m
//  OperationPro
//
//  Created by Mac on 16/10/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DiscussDetailCell.h"
#import "DiscussDetailModel.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
@interface DiscussDetailCell()
//@property (nonatomic, strong) UILabel *lblDate;
@property (nonatomic, strong) UILabel *lblDes;
@property (nonatomic, strong) UILabel *lblCount;
@property (nonatomic, strong) UIImageView *btnGood;
@property (nonatomic, strong) UIView *replyView;
@property (nonatomic, strong) UILabel *lblReply;
@property (nonatomic, strong) UIImageView *headImg;
@property (nonatomic, strong) UIImageView *topImg;
@property (nonatomic, strong) UIImageView *lineImg;

@end

@implementation DiscussDetailCell

- (void)initView{
    [super initView];
    _topImg=[[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 50, 50)];
    [self addSubview:_topImg];
    _topImg.layer.masksToBounds=YES;
    _topImg.layer.cornerRadius=25;

    //姓名和时间
    self.lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(75, 15, WindowWidth-75-15, 13)];
    [self addSubview:self.lblTitle];
    [self.lblTitle setTextColor:LightFontColor];
    [self.lblTitle setFont:F(10)];
    //时间
//    _lblDate = [[UILabel alloc] init];
//    [self addSubview:_lblDate];
//    [_lblDate mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.lblTitle);
//        make.right.equalTo(self).mas_offset(0);
//        make.left.equalTo(self.lblTitle.mas_right).offset(5);
//    }];
//    [_lblDate setTextColor:LightFontColor];
//    [_lblDate setFont:F(10)];
    
    self.headImg =[[UIImageView alloc]init];
    self.headImg.frame=CGRectMake(75, CGRectGetMaxY(self.lblTitle.frame)+5, 50, 50);
    [self addSubview:self.headImg];
    //内容
    _lblDes = [[UILabel alloc] initWithFrame:CGRectMake(75, CGRectGetMaxY(self.lblTitle.frame)+5, WindowWidth-75-50, 10)];
    [self addSubview:_lblDes];
    _lblDes.numberOfLines = 0;
    _lblDes.font = F(12);
    _lblDes.textColor = FontColor;
    
    UIImage *imgGood = [UIImage imageNamed:@"Ico_assist_canle"];
    //赞的点击
    UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-50, 25, 50, 30)];
    [self addSubview:bgView];

    //赞的数据
    _lblCount = [[UILabel alloc] initWithFrame:CGRectMake(imgGood.size.width+5, 0, 50-imgGood.size.width-5, 30)];
    [bgView addSubview:_lblCount];
    //    [_lblCount mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.bottom.equalTo(bgView).offset(-8);
    //        make.right.equalTo(bgView).mas_offset(-10);
    //    }];
    _lblCount.font = F(12);
    _lblCount.textColor = MainColor;
    [_lblCount setUserInteractionEnabled:YES];
    
    //赞
    _btnGood = [[UIImageView alloc] initWithFrame:CGRectMake(0, (30-imgGood.size.height)/2, imgGood.size.width, imgGood.size.height)];
    [bgView addSubview:_btnGood];
    //    [_btnGood mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.right.equalTo(self.lblCount.mas_left).mas_offset(-5);
    //        make.size.mas_equalTo(imgGood.size);
    //        make.bottom.equalTo(bgView).offset(-8);
    //    }];
    [_btnGood setUserInteractionEnabled:YES];
    [bgView bk_whenTapped:^{
        if (self.GoodAction) {
            self.GoodAction(self);
        }
    }];
    //引用别人的
    _replyView = [[UIView alloc] init];
    [self addSubview:_replyView];
    [_replyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblTitle);
        make.right.equalTo(self).mas_offset(-10);
        make.height.equalTo(@32);
        make.bottom.equalTo(self).mas_offset(-10);
    }];
    _replyView.layer.borderWidth = .5;
    _replyView.layer.borderColor = LineColor.CGColor;
    _replyView.hidden = YES;
    
    _lblReply = [[UILabel alloc] init];
    [_replyView addSubview:_lblReply];
    [_lblReply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_replyView).mas_offset(5);
        make.centerY.equalTo(_replyView);
        make.right.equalTo(_replyView).mas_offset(-5);
    }];
    _lblReply.textColor = self.lblTitle.textColor;
    _lblReply.font = self.lblTitle.font;
    
    _lineImg=[[UIImageView alloc]init];
    [self addSubview:_lineImg];
    UIImage *img=[UIImage imageNamed:@"Ico_common_line"];
    [_lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lblTitle);
        make.right.equalTo(self).offset(0);
        make.size.mas_equalTo(CGSizeMake(img.size.width, img.size.height));
        make.bottom.equalTo(self.mas_bottom).offset(-img.size.height);
        
    }];

    _lineImg.image=[UIImage imageNamed:@"Ico_common_line"];
}

- (void)setModel:(DiscussDetailModel *)model{
    NSString*urlString =[CClass getCacheData:qnDomain];
    if (model.D_headImg==nil||[model.D_headImg isEqualToString:@""]) {
        _topImg.image=[UIImage imageNamed:@"Ico_home_head"];
        
    }else
    {
        NSURL *headUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,model.D_headImg]];
        [_topImg sd_setImageWithURL:headUrl placeholderImage:[UIImage imageNamed:@"Ico_home_head"]];
        
    }
     
   //自己回复的时候没有图片时候
    if (model.D_image==nil||[model.D_image isEqualToString:@""]) {
        NSString *timeString = [model.D_created  substringWithRange:NSMakeRange(0, 16)];
        self.headImg.hidden=YES;
        self.lblTitle.text = [NSString stringWithFormat:@"%@ %@",model.D_author,timeString];
        CGSize descrideSize  = [model.D_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-75-50, 900)];

        self.lblDes.text = model.D_content;
        self.lblDes.frame=CGRectMake(75, CGRectGetMaxY(self.lblTitle.frame)+5,  WindowWidth-75-50,descrideSize.height);

        _lblCount.text = model.D_thumb_count;
        bool hasReply = model.D_refer_author != nil;
        bool replied = [model.D_thumb_by_me isEqualToString:@"1"];
       
        _lblCount.textColor = C(replied ? @"fd603d" : @"b7b4b4");
        _btnGood.image = [UIImage imageNamed:replied ? @"Ico_assist_up" : @"Ico_assist_canle"];
        
        _replyView.hidden = !hasReply;
        //被回复的人
        if (hasReply) {

            NSString *replyName = model.D_refer_author;
            if (model.D_refer_image==nil||[model.D_refer_image isEqualToString:@""]) {
                _lblReply.text = [NSString stringWithFormat:@"%@:%@", replyName, model.D_refer_content];
            }else
            {
                _lblReply.text = [NSString stringWithFormat:@"%@:[图片]%@", replyName, model.D_refer_content];
            }
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:_lblReply.text];
            [attr addAttributes:@{NSFontAttributeName:F(10), NSForegroundColorAttributeName:C(@"b7b4b4")} range:NSMakeRange(replyName.length + 1, _lblReply.text.length - replyName.length - 1)];
            _lblReply.attributedText = attr;

        }

    }else
    {
        //自己回复的时候有图片时候
        self.lblTitle.text = model.D_author;
        NSString *timeString = [model.D_created substringWithRange:NSMakeRange(0, 16)];
        self.lblTitle.text = [NSString stringWithFormat:@"%@ %@",model.D_author,timeString] ;
        self.headImg.hidden=NO;

        CGSize descrideSize  = [model.D_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-75-50, 900)];
        
        self.lblDes.text = model.D_content;
        self.lblDes.frame=CGRectMake(75, CGRectGetMaxY(self.headImg.frame)+5,  WindowWidth-75-50,descrideSize.height);

      
     
        NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[CClass getCacheData:qnDomain],model.D_image]];
        [self.headImg sd_setImageWithURL:url];
        _headImg.userInteractionEnabled=YES;
        [_headImg bk_whenTapped:^{
            [XWScanImage scanBigImageWithImageView:_headImg];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"closeKeyboard" object:nil];
        }];
        _lblCount.text = model.D_thumb_count;
        bool hasReply = model.D_refer_author != nil;
        bool replied = [model.D_thumb_by_me isEqualToString:@"1"];
        
        _lblCount.textColor = C(replied ? @"fd603d" : @"b7b4b4");
        _btnGood.image = [UIImage imageNamed:replied ? @"Ico_assist_up" : @"Ico_assist_canle"];
        
        _replyView.hidden = !hasReply;
        if (hasReply) {
            NSString *replyName = model.D_refer_author;
            if ([model.D_refer_image isEqualToString:@""]) {
               _lblReply.text = [NSString stringWithFormat:@"%@:%@", replyName, model.D_refer_content];
            }else
            {
                 _lblReply.text = [NSString stringWithFormat:@"%@:[图片]%@", replyName, model.D_refer_content];
            }
           
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:_lblReply.text];
            [attr addAttributes:@{NSFontAttributeName:F(10), NSForegroundColorAttributeName:C(@"b7b4b4")} range:NSMakeRange(replyName.length + 1, _lblReply.text.length - replyName.length - 1)];
            _lblReply.attributedText = attr;
        }
 
    }
    
}

+ (float)getCellHeightByModel:(DiscussDetailModel *)model{
       if (model.D_image == nil || [model.D_image isEqualToString:@""]) {
           CGFloat contextHeight;
           if (model.D_content==nil||[model.D_content isEqualToString:@""]) {
               contextHeight=0;
           }else
           {
               CGSize size = [model.D_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-75-50, WindowHeight)];
               contextHeight=size.height;
           }
           float height = contextHeight+38;
           bool hasReply = model.D_refer_author != nil;
        //是否有引用
           if (hasReply) {
               height += 42;
               
           }else{
               height += 10;
           }
           return  MAX(80, height);


    }else
    {
        
        bool hasReply = model.D_refer_author != nil;
        CGFloat contextHeight;
        if (model.D_content==nil||[model.D_content isEqualToString:@""]) {
            contextHeight=0;
        }else
        {
             CGSize size = [model.D_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-75-50, WindowHeight)];
            contextHeight=size.height;
        }
      
        //是否有引用
        float height = MAX(80, contextHeight+38+60);
        if (hasReply) {
            height += 42;
        }else{
            height += 10;
        }
        return height;

    }
}

@end
